<!--登录-->
<template>
  <div class="login-container">
    <div class="decorate-circle circle-1"></div>
    <div class="decorate-circle circle-2"></div>

    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h1>档案柜系统</h1>
        </div>
      </template>

      <el-form class="card-form">
        <el-form-item>
          <el-input
              v-model="form.account"
              placeholder="请输入用户名"
              :prefix-icon="User"
              size="large"
          />
        </el-form-item>

        <el-form-item>
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              size="large"
          />
        </el-form-item>
        <el-form-item>
          <div class="captcha-container">
            <el-input
                v-model="form.captcha"
                placeholder="请输入验证码"
                :prefix-icon="Key"
                size="large"
                class="captcha-input"
            />
            <div
                class="captcha-code"
                @click="generateCaptcha"
                v-html="captchaCode"
            ></div>
          </div>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="card-footer">
          <el-button
              type="success"
              class="login-btn"
              @click="userLogin"
          >
            <span>登 录</span>
          </el-button>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, onUnmounted, ref} from 'vue'
import { login } from "../api/login.js";
import { useRouter } from 'vue-router'
import {ElMessage} from "element-plus";
import {User, Lock, Key} from '@element-plus/icons-vue'
import {useTokenStore} from "../token/token.js";


const router = useRouter()

const captchaCode = ref('') // 验证码

const form = ref({
  account: "",
  password: "",
  captcha: "" // 验证码字段
});

const tokenStore=useTokenStore()

// 登录
const userLogin = () => {
  // 验证码核验
  if (!form.value.captcha || form.value.captcha.trim() === '') {
    ElMessage.warning('请输入验证码')
    return
  }
  if (form.value.captcha.toUpperCase() !== captchaCode.value) {
    ElMessage.error('验证码输入错误')
    generateCaptcha() // 刷新验证码
    return
  }
  // 登录
  login({
    account: form.value.account,
    password: form.value.password
  }).then((res) => {
    console.log(res)
    if (res.code === 1) {
      tokenStore.setToken(res.data.token)
      router.push('/home')
      ElMessage.success("登录成功");
    } else {
      ElMessage.warning("登录失败，请检查用户名和密码");
    }
  }).catch((err) => {
    ElMessage.error(err.msg)
  });
};

// 生成4位随机验证码
const generateCaptcha = () => {
  captchaCode.value = Math.random().toString(36).substr(2, 4).toUpperCase()
}

onMounted(() => {
// 初始化验证码
  generateCaptcha()
// 监听回车键登录
  window.addEventListener('keydown', handleEnterKey);
});

// 离开组件时移除监听器（避免内存泄漏）
onUnmounted(() => {
  window.removeEventListener('keydown', handleEnterKey);
});

// 处理回车键事件
const handleEnterKey = (e) => {
  if (e.key === 'Enter') {
    userLogin();
  }
};
</script>



<style scoped>
.login-container {
  min-height: 100vh;
  min-width: 100vw;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
}

.decorate-circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(58,194,159,0.1) 0%, rgba(58,123,213,0.1) 100%);
  filter: blur(30px);
}

.circle-1 {
  width: 400px;
  height: 400px;
  top: -100px;
  left: -100px;
}

.circle-2 {
  width: 350px;
  height: 350px;
  bottom: -150px;
  right: -100px;
}

.login-card {
  width: 480px;
  border-radius: 20px;
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1;
}

.login-card:hover {
  transform: translateY(-5px) scale(1.02);
}

.card-header {
  text-align: center;
  padding: 40px 0 20px;
}

.logo {
  width: 80px;
  margin-bottom: 20px;
  filter: drop-shadow(0 2px 4px rgba(58, 194, 159, 0.2));
}

.card-header h1 {
  color: #2c3e50;
  font-size: 24px;
  line-height: 1.4;
  margin: 0;
  font-weight: 600;
  letter-spacing: 1px;
}

.card-form {
  padding: 0 40px 20px;
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  letter-spacing: 2px;
  background: linear-gradient(135deg, rgba(79, 198, 242, 0.83) 0%, #3a7bd5 100%);
  transition: all 0.3s ease;
}

.login-btn span {
  position: relative;
  z-index: 1;
}

:deep(.el-input__inner) {
  height: 48px;
  border-radius: 12px!important;
  padding-left: 40px!important;
}

:deep(.el-input__prefix) {
  left: 12px;
  font-size: 18px;
  color: #4FC6F2D3;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-input {
  flex: 1;
}

.captcha-code {
  width: 120px;
  height: 40px;
  background: #f0f2f5;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Arial';
  font-size: 24px;
  font-weight: bold;
  color: #4FC6F2D3;
  cursor: pointer;
  user-select: none;
  position: relative;
  overflow: hidden;
}

.captcha-code::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background: repeating-linear-gradient(
      -45deg,
      transparent 0,
      transparent 5px,
      rgba(58,194,159,0.1) 5px,
      rgba(58,194,159,0.1) 10px
  );
}

.captcha-code:hover {
  background: #e8eaf1;
}

/* 响应式优化 */
@media (max-width: 480px) {
  .login-card {
    width: 90%;
    margin: 20px;
  }
  .card-form {
    padding: 0 20px 20px;
  }
}
</style>